package com.gusha.controller;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;
import com.gusha.service.SysMenuService;
import com.gusha.service.SysRoleService;
import com.gusha.dao.SysRoleDao;
import com.gusha.dao.SysRoleMenuDao;
import com.gusha.model.SysMenu;
import com.gusha.model.SysRole;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysRoleController控制类
 */
@Controller
@RequestMapping("/gx/sysRole")
public class SysRoleController {

    // 注入Dao
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    
    // 注入Service
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;

    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看角色列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String roleName = WebValueUtil.getWebValue(request, "roleName");

        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        SysRole sysRole = new SysRole();
        if (StringUtils.isNotBlank(roleName)) {
        	sysRole.setRoleName(roleName);
        }

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<SysRole> page = sysRoleService.findAll(pageable, sysRole);

        model.addAttribute("page", page);
        model.addAttribute("roleName", roleName);
        return "/admin/sysRole/list";
    }

    // 进入添加页面
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入角色添加页面")
    public String toAdd(Model model) {
    	List<SysMenu> list = sysMenuService.getOneAndTwoMenuListByAll();
		model.addAttribute("allMenuList", list);
		List<Integer> listRole = new ArrayList<Integer>();
		model.addAttribute("allMenuRole", listRole);
        return "/admin/sysRole/form";
    }

    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入角色编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");
        List<SysMenu> list = sysMenuService.getOneAndTwoMenuListByAll();
		model.addAttribute("allMenuList", list);
        if (StringUtils.isNotBlank(id)) {
            if(sysRoleDao.existsById(new BigInteger(id))) {
                SysRole sysRole = sysRoleDao.getOne(new BigInteger(id));
                model.addAttribute("role_get", sysRole);
        		List<BigInteger> listRole = sysRoleMenuDao.findAllMenuByRole(sysRole);
    			model.addAttribute("allMenuRole", listRole);
            }
        }        

        return "/admin/sysRole/form";
    }

    // 保存
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存角色信息")
    public JsonResult save(SysRole sysRole,String menuIds) {
    	JsonResult result = sysRoleService.saveRole(sysRole, menuIds);
		return result;
    }


    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除角色信息")
    public JsonResult delete(BigInteger id) {
    	JsonResult result = sysRoleService.deleteRole(id);
		return result;
    }

}
